package com.david.service.Impl;

import com.david.dao.ConsoleMapper;
import com.david.entity.Message;
import com.david.service.ConsoleInterface;
import com.david.vo.CardBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author David
 */
@Service
public class ConsoleService implements ConsoleInterface {

    @Resource
    ConsoleMapper consoleMapper;

    @Override
    public CardBean countConsole(){
        CardBean cardBean = new CardBean();
        Map<String, String> temp = new HashMap<>(4);
        temp.put("value1", String.valueOf(consoleMapper.countEmployee()));
        temp.put("value2", String.valueOf(consoleMapper.countDepartment()));
        temp.put("value3", String.valueOf(consoleMapper.countMan("男") / consoleMapper.countMan("女")));
        temp.put("value4", String.valueOf(consoleMapper.countPolitics("中共党员")));
        cardBean.success(temp);
        cardBean.setCount(4);

        return cardBean;
    }

    @Override
    public CardBean addMessage(Message message){
        if ("".equals(message.getFile())){
            message.setFile("../../admin/images/88.jpg");
        }else{
            String file = message.getFile().substring(12);
            String filePath = "../../admin/images/";
            message.setFile(filePath + file);
        }
        consoleMapper.addMessage(message);
        CardBean cardBean = new CardBean();
        cardBean.success("留言成功！");
        cardBean.setMsg("留言成功！");
        return cardBean;
    }

    @Override
    public CardBean getMessage(Integer page, Integer limit){
        CardBean cardBean = new CardBean();
        cardBean.setCode(1);
        cardBean.setMsg("获取留言成功！");

        PageHelper.startPage(page, limit);
        List<Message> messageList = consoleMapper.getMessage();
        cardBean.setData(messageList);

        PageInfo<Message> messagePageInfo = new PageInfo<>(messageList);
        cardBean.setCount((int)(messagePageInfo.getTotal()));

        return cardBean;
    }

    @Override
    public CardBean uploadImage(MultipartFile file){
        CardBean baseBean = new CardBean();
        String originalFileName = file.getOriginalFilename();
        // 随机修改名字
//        String fileName = System.currentTimeMillis() + "." + originalFileName.substring(originalFileName.lastIndexOf(".") + 1);

        // 服务器上的存储上传的图片的地址
        String filePath = "/home/lighthouse/ww/images/";

        // 本机上的存储上传的图片的地址
//        String filePath = "D:\\images\\";

        File dest = new File(filePath + originalFileName);
        // 文件夹不存在，则创建文件夹
        if(!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        // 捕获复制文件出现的异常
        try{
            file.transferTo(dest);
        }catch (Exception e){
            e.printStackTrace();
        }
        baseBean.setCode(0);
        baseBean.setMsg("上传成功！");
        return baseBean;
    }
}
